package com.woody.aggregate.app.exception;

import com.woody.aggregate.application.exception.BusinessException;
import com.woody.base.constant.HttpCodeEnum;
import com.woody.base.exception.BaseException;
import com.woody.base.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常统一处理
 *
 */
@Order
@ControllerAdvice
@Slf4j
public class ExceptionResolver {

    /**
     * @param e:
     * @Version 1.0
     * @Description 处理业务错误
     */
    @ExceptionHandler(BaseException.class)
    public ResponseEntity<Object> baseExceptionHandler(BaseException e) {
        String text = String.format("%s:%s", e.getClass().getSimpleName(), e.getMessage());
        log.warn(text, e);
        Response response = new Response(e.getCode(), e.getMsg(), null);
        return ResponseEntity.status(HttpCodeEnum.OK.getCode()).body(response);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> exceptionResolver(Exception e) {
        String text = String.format("%s:%s", e.getClass().getSimpleName(), e.getMessage());
        if (e instanceof NoHandlerFoundException) {
            log.error(text, e);
            return ResponseEntity.status(HttpCodeEnum.OK.getCode()).body(Response.fail(text));
        } else if (e instanceof MethodArgumentNotValidException) {
            log.error("校验失败", e);
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
            BindingResult bindingResult = methodArgumentNotValidException.getBindingResult();
            StringBuilder sb = new StringBuilder("校验失败:");
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                sb.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append("; ");
            }
            String msg = sb.toString();
            return ResponseEntity.status(HttpCodeEnum.OK.getCode()).body(Response.fail(msg));
        } else {
            log.error(text, e);
            return ResponseEntity.status(HttpCodeEnum.OK.getCode()).body(Response.fail(HttpCodeEnum.SERVER_ERROR));
        }
    }

    /**
     * @param e:
     * @Version 1.0
     * @Description 处理业务错误
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<Object> businessExceptionHandler(BusinessException e) {
        String text = String.format("%s:%s", e.getClass().getSimpleName(), e.getMessage());
        log.warn(text, e);
        Response response = new Response(e.getCode(), e.getMsg(), null);
        return ResponseEntity.status(HttpCodeEnum.OK.getCode()).body(response);
    }
}
